<?php
class Request{

	/**
	 * parameters
	 */
	private $_params = array();

	/**
	 * instance
	 */
	private static $_instance;

	/**
	 * self::instance
	 */
	public static function getInstance(){

		if( NULL == self::$_instance){
			self::$_instance = new Request();
		}
		return self::$_instance;
	}

	public function setParams($params){
		//处理字符串
		if (!is_array($params)) {
			parse_str($params, $out);
			$params = $out;
		}

		$this->_params = array_merge($this->_params, $params);
	}

	public function setParam($name, $value){
		$this->_params[$name] = $value;
	}

	public function getParam($key){
		//return htmlspecialchars( $this->get($key) );
		return $this->get($key);
	}


	public function get($key, $default = NULL){   
		$value = $default;

		switch (true) {
			case isset($this->_params[$key]):
				$value = $this->_params[$key];
				break;
			case isset($_GET[$key]):
				$value = $_GET[$key];
				break;
			case isset($_POST[$key]):
				$value = $_POST[$key];
				break;
			case isset($_COOKIE[$key]):
				$value = $_COOKIE[$key];
				break;
			default:
				$value = $default;
				break;
		}   
		return is_array($value) || strlen($value) > 0 ? $value : $default;
	}   

	/**
	 * get server header
	 */
	public function getHeader( $name ){

		$value = NULL;
		if (isset($_SERVER[$name]))
			$value = $_SERVER[$name];
		else if (isset($_ENV[$name]))
			$value = $_ENV[$name];

		return $value;
	}

	/**
	 * get request method
	 * return string
	 */
	public function getMethod(){
		return $this->getHeader('REQUEST_METHOD');
	}

	/**
	 * is post's method
	 * return bool
	 */
	public function isPost(){

		return "POST" == $this->getMethod();

	}

	/**
	 * is get's method
	 * return bool
	 */
	public function isGet(){

		return "GET" == $this->getMethod();

	}

	/**
	 *
	 */
	public function isAjax(){
	
			return 'XMLHttpRequest' == $this->getHeader('HTTP_X_REQUESTED_WITH');

	}

	public function getReferer(){
	
		return $this->getHeader('HTTP_REFERER');

	}

	/**
	 * get cookie
	 */
	public function getCookie($key = null, $default = null){
		if (null === $key) {
			return $_COOKIE;
		}
		return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
	}

	/**
	 * get True ip
	 */
	public function getIp(){
		if ($this->getHeader('HTTP_CLIENT_IP') != null) {
			$ip = $this->getHeader('HTTP_CLIENT_IP');
		} else if ($this->getHeader('HTTP_X_FORWARDED_FOR') != null) {
			$ip = $this->getHeader('HTTP_X_FORWARDED_FOR');
		} else {
			$ip = $this->getHeader('REMOTE_ADDR');
		}    
		return $ip;
	}

	/**
	 *
	 */
	public function getAgent(){
		return $this->getHeader('HTTP_USER_AGENT');
	}

	public function isIe(){
		$agent = $this->getAgent();
		return strpos($agent,"MSIE")?true:false;
	}

	public function isFirefox(){
		$agent = $this->getAgent();
		return strpos($agent, "Firefox")?true:false;
	}
	
	public function isSafari(){
		$agent = $this->getAgent();
		return strpos($agent, "Safari")?true:false;
	}

	public function notSupportBrowser(){
		$agent = $this->getAgent();
		if($this->isIe()){
			if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)){
				  $browser_ver = $regs[1];
					if(strlen($browser_ver) > 1){
						$first = intval(substr($browser_ver,0 ,1));
						if($first < 8){
							return true;
						}
					}
			}
		}
		return false;
	}
}
